package com.zry.simpleBlog.controller.admin;

import com.zry.simpleBlog.comment.aop.annotations.AuthCheck;
import com.zry.simpleBlog.comment.aop.annotations.Idempotent;
import com.zry.simpleBlog.comment.aop.annotations.LogWeb;
import com.zry.simpleBlog.comment.enums.RoleEnum;
import com.zry.simpleBlog.comment.enums.IdempotentStrategyEnum;
import com.zry.simpleBlog.comment.enums.RespBeanEnum;
import com.zry.simpleBlog.comment.respBean.RespBean;
import com.zry.simpleBlog.dto.BlogQuery;
import com.zry.simpleBlog.dto.PostBlogDTO;
import com.zry.simpleBlog.service.IBlogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 后台管理文章的接口
 *
 * @author zry
 * @since 2022-04-09
 */

@AuthCheck(rank = RoleEnum.GOD)
@RestController
@Tag(name = "后台文章管理接口")
@RequestMapping("/admin")
public class AdminBlogController {

    @Resource
    private IBlogService blogService;

    /**
     * 后台博客分页
     *
     * @return mv
     */
    @AuthCheck(rank = RoleEnum.USER)
    @Operation(summary = "后台文章分页", description = "可按条件查询")
    @GetMapping("/blogs")
    public RespBean blogs(@Parameter(name = "pageSize", description = "页大小") @RequestParam(defaultValue = "8") Integer pageSize,
                          @Parameter(name = "pageNum", description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
                          BlogQuery query) {
        return blogService.adminBlogPage(pageNum, pageSize, query);
    }

    /**
     * 新增文章
     */
    @Idempotent(timeout = 5, strategy = IdempotentStrategyEnum.IDEMPOTENT_INTERFACE)
    @Operation(summary = "添加文章", description = "实现幂等性")
    @PostMapping("/blogs")
    @LogWeb(title = "博客管理", action = "新增文章")
    public RespBean post(@RequestBody @Valid PostBlogDTO blogDto) {
        return blogService.saveBlog(blogDto);
    }

    /**
     * 更新文章
     */
    @Operation(summary = "更新文章")
    @PutMapping("/blogs/{id}")
    @LogWeb(title = "博客管理", action = "更新文章")
    public RespBean update(@RequestBody @Valid PostBlogDTO blogDto, @PathVariable String id) {
        blogDto.setId(id);
        return blogService.updateBlog(blogDto);
    }

    /**
     * 删除博客
     */
    @Operation(summary = "删除文章")
    @DeleteMapping("/blogs/{id}")
    public RespBean delete(@PathVariable String id) {
        blogService.deleteById(id);
        return RespBean.success(RespBeanEnum.DELETE_SUCCESS);
    }
}
